<?php
namespace TestAdmin\Controller;

use Common\Controller\OrgnizerCommonController;
use Think\Controller;


class OrgnizerController extends OrgnizerCommonController {

    public function _construct()
    {

//        $orgnizers = M('orgnizers');
//        $oid = $orgnizers->where(['uid' => session('uid')])->field('oid')->select();
//        $this->oid = $oid[0]['oid'];
//
//        if (!$this->oid){
//            echo "Invalid orgnizer id";
//            exit;
//        }

        parent::__construct();

    }


    public function result_content()
    {
        $this->display();
    }

    public function userclass_content()
    {
        $this->display();
    }

    public function wizard_content()
    {
        $this->display();
    }


    /**
     * Orgnizer主方法在OrgnizerCommonController
     */

    /**
     * 管理员组织者方法
     * @param $oid
     */
    public function setOid($oid)
    {
        session("adminoid", $oid);
        parent::__construct(session("adminoid"));
        $this->ajaxReturn(1);
    }

    public function deleteOrgnizer($oid)
    {
        $uidlist = M("orgnizeruserclass")->where(['oid' => $oid])->select();
        $status = D("Users")->deleteUsers($uidlist);
        if ($status['code'] == 1){
            M("orgnizers")->where(['oid' => $oid])->delete();
            M("orgnizer_grouptest")->where(['oid' => $oid])->delete();
            $this->ajaxReturn(['code' => 1, 'msg'=>"删除成功"]);
        }

    }

    public function addOrgnizer()
    {
        $userdata['username'] = I("username");
        $userdata['password'] = I("userpasswd");
        $userdata['email'] = I("email");
        $userdata['age'] = I("age");
        $userdata['sex'] = I("sex");
        if($userdata['sex'] == '男')
            $userdata['sex'] = 'male';
        else
            $userdata['sex'] = 'female';


        $orgnizerdata['orgnizer_name'] = I("orgnizername");
        $orgnizerdata['orgnizer_info'] = I("orgnizerinfo");

        $user = D("Users");
        if (!$data = $user->create($userdata)) {
            // 防止输出中文乱码
            header("Content-type: text/html; charset=utf-8");
            $rs['code'] = -1;
            $rs['msg'] = $user->getError();

        }

        else{
            $uid = $user->add($data);
            $orgnizerdata['uid'] = $uid;
            if (M("orgnizers")->add($orgnizerdata)){
                $rs['code'] = 1;
                $rs['msg'] ="添加成功！";
            }
            else{
                $rs['code'] = -1;
                $rs['msg'] ="添加失败！";
            }

        }

        $this->ajaxReturn($rs);

    }



}